package com.errol.batchprocessor.payroll.advisorimpl.common;

import com.renjia.enums.PayChannelEnum;
import com.renjia.enums.PayTipTypeEnum;
import com.renjia.enums.UserSourceTypeEnum;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeBatchPayload;
import com.renjia.service.module.payroll.advisor.upload.PayrollUploadCheckAdvisor;
import com.renjia.service.module.payroll.advisor.upload.PayrollUploadCreateAdvisor;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadBatchPayload;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadSinglePayload;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

/**
 * 代发与发放渠道扩展
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/03/17
 * @since v5.0.6
 **/
@Service
public class PayTypeAdvisorImpl implements
        PayrollUploadCheckAdvisor, PayrollUploadCreateAdvisor {

    @Override
    public boolean shallProcess(@NonNull PayrollEmployeeBatchPayload advisorPayload) {
        return true;
    }

    @Override
    public int priority() {
        return 102;
    }

    @Override
    public boolean doCheckBeforePayrollSavedAtUploadStage(@NonNull PayrollUploadBatchPayload batchPayload) {
        if (StringUtils.isBlank(batchPayload.getCoreChannelMerchant().getPayType())) {
            batchPayload.getErrMsgList().add("服务商未设置支付方式");
            return false;
        }
        // 当前没有二级渠道商，应该都不允许
        if (StringUtils.isNotBlank(batchPayload.getCoreCustomerChannelMerchant().getUpperChannelMerchantId())) {
            batchPayload.getErrMsgList().add("上级渠道商代发功能开发中");
            return false;
        }
        return true;
    }

    @Override
    public boolean doAfterNewPayrollRecordAtUploadStage(@NonNull PayrollUploadBatchPayload batchPayload) {
        // 支付渠道类型
        String payType = batchPayload.getCoreChannelMerchant().getPayType();
        if (Boolean.TRUE.equals(batchPayload.getCoreChannelMerchant().getWechatFlag())) {
            payType = PayChannelEnum.WECHAT.getCode().toString();
        }
        // 代发类型
        PayTipTypeEnum payTipType = PayTipTypeEnum.CUSTOMER;
        if (!batchPayload.getCoreUser().getUserSourceType().equals(UserSourceTypeEnum.CUSTOMER.getCode())) {
            if (batchPayload.getCoreUser().getUserSourceType().equals(UserSourceTypeEnum.PLATFORM.getCode())) {
                payTipType = PayTipTypeEnum.PLATFORM;
            } else {
                payTipType = PayTipTypeEnum.CHANNEL_MERCHANT;
            }
        }
        // 代发类型赋值
        batchPayload.getBatchEntity().setPayType(payType);
        batchPayload.getBatchEntity().setPayTip(PayTipTypeEnum.CUSTOMER != payTipType);
        batchPayload.getBatchEntity().setPayTipType(payTipType.getCode());
        return true;
    }

    @Override
    public boolean doAfterNewPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        // 代发类型赋值
        singlePayload.getBusPayrollRecordDetail().setPayType(batchPayload.getBatchEntity().getPayType());
        singlePayload.getBusPayrollRecordDetail().setPayTip(batchPayload.getBatchEntity().getPayTip());
        singlePayload.getBusPayrollRecordDetail().setPayTipType(batchPayload.getBatchEntity().getPayTipType());
        // 支付渠道 payChannel 应该可以认为是无效的了
        singlePayload.getBusPayrollRecordDetail().setPayChannel("0");
        return true;
    }

    @Override
    public boolean doBeforeModifyPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        return true;
    }
}
